<template>
  <div>
    试卷答案
  </div>
</template>

<script>
import http from '@/plugins/http'
import '@/mock/Rule'
export default {
  props: {
    dialogFormTitle: String,
    dialogId: Number
  },
  name: 'rule-edit',
  data () {
    return {
      fullscreen: false,
      dialogFormVisible: false,
      dialogLastOperation: 'add',
      operForm: {
        rule_id: 0,
        rule_name: '',
        rule_title: '',
        rule_condition: ''
      },
      rules: {
        rule_name: [{ required: true, message: '请输入名称', trigger: 'change' }],
        rule_title: [{ required: true, message: '请输入规则', trigger: 'change' }]
      }
    }
  },
  methods: {
    submitForm: function (formName) {
      const self = this
      this.$refs[formName].validate(valid => {
        if (valid) {
          const url = self.$parent.dialogId === 0 ? '/Rule/add' : '/Rule/edit'
          http.send({ sendType: 'post', url: url, param: this.operForm, showSuccessTip: true }).then(data => {
            if (data.code === 0) {
              self.$refs[formName].resetFields()
              self.toggleDialog(false)
              self.$parent.loadMine()
            }
          })
        }
      })
    },
    toggleDialog: function (flag) {
      this.dialogFormVisible = flag
    },
    toggleFullscreen: function () {
      this.fullscreen = !this.fullscreen
    },
    handleAction: function () {
      this.operForm.rule_name = '【操作】XXX==XXX'
      this.operForm.rule_title = 'Action-XXX-XXX'
    },
    handleMenu: function () {
      this.operForm.rule_name = '【菜单】XXX==XXX'
      this.operForm.rule_title = 'Menu-XXX-XXX'
    },
    handleElement: function () {
      this.operForm.rule_name = '【元素】XXX==XXX'
      this.operForm.rule_title = 'Element-XXX-XXX'
    }
  },
  watch: {
    '$parent.dialogEditTime': function () {
      if (this.$parent.dialogId === 0) {
        if (this.dialogLastOperation === 'edit') {
          this.$nextTick(() => {
            this.$refs.operForm.resetFields()
          })
          this.dialogLastOperation = 'add'
        }
      } else {
        this.dialogLastOperation = 'edit'
        this.$nextTick(() => {
          this.$refs.operForm.resetFields()
        })
        http.send({ url: '/Rule/detail', param: { params: { id: this.$parent.dialogId } } }).then(data => {
          this.operForm = data.extra
        })
      }
    }
  }
}
</script>
